//
// Created by lz on 3/1/21.
//

#ifndef MYWEBLIB_BASE_POLLER_H_
#define MYWEBLIB_BASE_POLLER_H_

#include <map>
#include <vector>

#include "base/Timestamp.h"
#include "net/EventLoop.h"

namespace lzweb
{
	namespace net
	{
		class Channel;

		///
		/// Base class for IO Multiplexing
		///
		/// This class doesn't own the Channel objects.
		class Poller : noncopyable
		{
		 public:
			using ChannelList = std::vector<Channel*>;

			Poller(EventLoop* loop);

			virtual ~Poller();

			/// Polls the I/O events.
			/// Must be called in the loop thread.
			virtual TimeStamp poll(int timeoutMs, ChannelList* activeChannels) = 0;

			/// Changes the interested I/O events.
			/// Must be called in the loop thread.
			virtual void updateChannel(Channel* channel) = 0;

			/// Remove the channel, when it destructs.
			/// Must be called in the loop thread.
			virtual void removeChannel(Channel* channel) =0;

			virtual bool hasChannel(Channel* channel) const;

			static Poller* newDefaultPoller(EventLoop* loop);

			void assertInLoopThread() const
			{
				ownerLoop_->assertInLoopThread();
			}



		 protected:
			using ChannelMap = std::map<int, Channel*>;
			ChannelMap channels_;   //这个存储着Poller监测的fd及其事件

		 private:
			EventLoop* ownerLoop_;

		};
	}// namespace net
}//namespace lzweb



#endif //MYWEBLIB_BASE_POLLER_H_
